
/**
 * @file main
 *
 */

/*********************
 *      INCLUDES
 *********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdlib.h>
#include <unistd.h>
#define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" issue*/
#include <SDL2/SDL.h>
#include "lvgl/lvgl.h"
#include "lvgl/examples/lv_examples.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/sdl/sdl.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void hal_init(void);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *      VARIABLES
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
void demo(){

  //获取当前屏幕对象
  lv_obj_t* screen = lv_scr_act();
  //创建对象放入屏幕中
  lv_obj_t* obj = lv_obj_create(screen);
  //设置大小，宽度
  //lv_obj_set_pos(obj,30,50);
  //lv_obj_set_width(obj,30);
}

void demo2(){
  // 获取当前屏幕对象
  lv_obj_t *screen = lv_scr_act();
  // 创建对象放入屏幕中
  lv_obj_t *obj = lv_obj_create(screen);
  static lv_style_t sty;
  lv_style_init(&sty);
  lv_style_set_width(&sty,50);
  lv_style_set_height(&sty,50);
  lv_style_set_align(&sty,LV_ALIGN_CENTER);
  lv_style_set_bg_color(&sty,lv_color_hex(0xfeacc2));
  lv_obj_add_style(obj,&sty,0);

}

void create_Label(){

  //声明字体
  LV_FONT_DECLARE(alimama);

  lv_obj_t* screen = lv_scr_act();
  lv_obj_t* label = lv_label_create(screen);
  //设置文本内容
  //lv_label_set_text(label,"icheima");
  
  lv_label_set_text(label,"重生之在黑马学LVGL");

  // 设置对齐方式
  lv_obj_set_align(label, LV_ALIGN_CENTER);
  // 设置字体
  lv_obj_set_style_text_color(label, lv_color_hex(0xfeacc2), 0);

  static lv_style_t style;
  lv_style_init(&style);

  lv_style_set_text_font(&style, &alimama);

  //滚动
  lv_label_set_long_mode(label,LV_LABEL_LONG_SCROLL);
  lv_obj_set_width(label,200);
  
  lv_obj_add_style(label,&style,0);
}

void demo_img(){

  //声明图片
  LV_IMG_DECLARE(ironman);
  //创建图片对象
  lv_obj_t * screen = lv_scr_act();
  lv_obj_t*img = lv_img_create(screen);

  //将给对象赋图片
  lv_img_set_src(img,&ironman);

}

void demo_gif(){

  //声明gif图片
  LV_IMG_DECLARE(astro);
  lv_obj_t* screen = lv_scr_act();
  //设置gif对象
  lv_obj_t* gif = lv_gif_create(screen);
  lv_gif_set_src(gif,&astro);
}

//按键回调函数
void event_handle(lv_event_t* event){
  

  int code = lv_event_get_code(event);
  lv_obj_t* obj = lv_event_get_target(event);

  if(code == LV_EVENT_CLICKED){
    printf("obj=%p\n",obj);
    printf("clicked\n");
   
  }


}

void demo_btn(){

  lv_obj_t* screen = lv_scr_act();
  lv_obj_t* button = lv_btn_create(screen);
  printf("btn=%p\n",button);
  //添加文本到按键上
  lv_obj_t* label = lv_label_create(button);
  lv_label_set_text(label,"click me");
  lv_obj_center(button);

  //给按键添加回调函数
  lv_obj_add_event_cb(button,event_handle,LV_EVENT_CLICKED,NULL);
}

int main(int argc, char **argv)
{
  (void)argc; /*Unused*/
  (void)argv; /*Unused*/

  /*Initialize LVGL*/
  lv_init();

  /*Initialize the HAL (display, input devices, tick) for LVGL*/
  hal_init();

  //demo();
  //demo2();
  //create_Label();

  //demo_img();
 // demo_gif();

 demo_btn();

  // lv_btn_create(lv_scr_act());
  // lv_obj_t *btn = lv_button_create(lv_screen_active());
  //  lv_example_switch_1();
  //  lv_example_calendar_1();
  //  lv_example_btnmatrix_2();
  //  lv_example_checkbox_1();
  //  lv_example_colorwheel_1();
  //  lv_example_chart_6();
  //  lv_example_table_2();
  //  lv_example_scroll_2();
  //  lv_example_textarea_1();
  //  lv_example_msgbox_1();
  //  lv_example_dropdown_2();
  //  lv_example_btn_1();
  //  lv_example_scroll_1();
  //  lv_example_tabview_1();
  //  lv_example_tabview_1();
  //  lv_example_flex_3();
  //  lv_example_label_1();
 
  //  lv_demo_widgets();


  while(1) {
      /* Periodically call the lv_task handler.
       * It could be done in a timer interrupt or an OS task too.*/
      lv_timer_handler();
      usleep(5 * 1000);
  }

  return 0;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/**
 * Initialize the Hardware Abstraction Layer (HAL) for the LVGL graphics
 * library
 */
static void hal_init(void)
{
  /* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/
  sdl_init();

  /*Create a display buffer*/
  static lv_disp_draw_buf_t disp_buf1;
  static lv_color_t buf1_1[SDL_HOR_RES * 100];
  lv_disp_draw_buf_init(&disp_buf1, buf1_1, NULL, SDL_HOR_RES * 100);

  /*Create a display*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  disp_drv.draw_buf = &disp_buf1;
  disp_drv.flush_cb = sdl_display_flush;
  disp_drv.hor_res = SDL_HOR_RES;
  disp_drv.ver_res = SDL_VER_RES;

  lv_disp_t * disp = lv_disp_drv_register(&disp_drv);

  lv_theme_t * th = lv_theme_default_init(disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK, LV_FONT_DEFAULT);
  lv_disp_set_theme(disp, th);

  lv_group_t * g = lv_group_create();
  lv_group_set_default(g);

  /* Add the mouse as input device
   * Use the 'mouse' driver which reads the PC's mouse*/
  static lv_indev_drv_t indev_drv_1;
  lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
  indev_drv_1.type = LV_INDEV_TYPE_POINTER;

  /*This function will be called periodically (by the library) to get the mouse position and state*/
  indev_drv_1.read_cb = sdl_mouse_read;
  lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

  static lv_indev_drv_t indev_drv_2;
  lv_indev_drv_init(&indev_drv_2); /*Basic initialization*/
  indev_drv_2.type = LV_INDEV_TYPE_KEYPAD;
  indev_drv_2.read_cb = sdl_keyboard_read;
  lv_indev_t *kb_indev = lv_indev_drv_register(&indev_drv_2);
  lv_indev_set_group(kb_indev, g);

  static lv_indev_drv_t indev_drv_3;
  lv_indev_drv_init(&indev_drv_3); /*Basic initialization*/
  indev_drv_3.type = LV_INDEV_TYPE_ENCODER;
  indev_drv_3.read_cb = sdl_mousewheel_read;
  lv_indev_t * enc_indev = lv_indev_drv_register(&indev_drv_3);
  lv_indev_set_group(enc_indev, g);

  /*Set a cursor for the mouse*/
  LV_IMG_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
  lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
  lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
  lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/
}
